package com.jinyu.jyly.base;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.jinyu.jyly.reveiver.NetWorkChangReceiver;
import com.jinyu.jyly.utils.DisplayCutoutUtil;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.ButterKnife;


/**
 * @Description activity的基类
 */
public abstract class BaseActivity<T extends IPresenter> extends AppCompatActivity {

    public Context mContext;
    private T mPresenter;
    private Dialog mDialog;
    public Bundle mSavedInstanceState;
    public QMUITipDialog mTipDialog;
    private int mSb_h;
    public boolean mIsNeed;
    private boolean isRegistered = false;
    private NetWorkChangReceiver netWorkChangReceiver;

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics() );
        return res;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSavedInstanceState = savedInstanceState;
        setContentView(getLayout());
        setStatusBar();
//        setNetWorkChange();
        mContext = this;
        ButterKnife.bind(this);

        mTipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载")
                .create();

        mPresenter = getPresenter();
        initEventAndData();
        setFloatingView(); //设置悬浮框
    }



    protected void setFloatingView() {

    }

    private void setNetWorkChange() {
        //注册网络状态监听广播
        netWorkChangReceiver = new NetWorkChangReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkChangReceiver, filter);
        isRegistered = true;
    }

    protected void setStatusBar() {
        //适配刘海
        mIsNeed = DisplayCutoutUtil.needAdaptNotch(this);
    }

    public void showDialogLoading() {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void disMissLoading() {
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }

    //获取布局
    public abstract int getLayout();

    //获取presenter
    protected abstract T getPresenter();

    //初始化事件和数据
    protected abstract void initEventAndData();

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }

        //解绑
        if (isRegistered) {
            unregisterReceiver(netWorkChangReceiver);
        }
    }

    /**
     * 弹吐司
     */
    public void showToast(String connect) {
        Toast.makeText(mContext, connect, Toast.LENGTH_SHORT).show();
    }

    /**
     * 关闭软键盘
     */
    public void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }




}
